function getNextAvailableServices() {
    var ticketId    =   $('#ticketIdInput').val();
    $.ajax({
		type:   "GET",
		url:    "../OutPatient/getNextAvailableServicesAjax?set_ajax_view",
		data:   "ticketId=" + ticketId,
		dataType:   "json",
		beforeSend: function(){},
		success:    function(data){
		    if (data.error.code == 'OK') {
                var buttons = {};
                var height	=	300;
                buttons[_('Cancel')] = function(){
                    $(this).dialog('close');
                };
                if (data.result.code == 'OK') {
                    buttons[_('Ok')] = function(){
                        performNextService($(this), ticketId);
                    };
                }
                else {
                    buttons[_('Add service')] = function(){
                        addService($(this));
                    };
                    buttons[_('Finish ticket')] = function(){
                        finishTicket(ticketId, $(this));
                    };
                    height	=	200;
                }
                $('#ticketDialogDiv').html(data.result.html);
                generalDialog('ticketDialogDiv', _("Next service"), null, null,{
	              autoOpen: true,         
	              bgiframe: true,
	              resizable: false,
	              modal: true,
	              width: 500,
	              height: height,
	              buttons: buttons
	           });
            }
            else {
                displayError(data.error.mess);              
            }
		}
    });
}

function performNextService(objDialog, ticketId) {
    if (!isReadyToSubmitNextService()) {
        return;
    }
    var submitted   =   $('#nextServiceFrm').formSerialize();
    $.ajax({
        type:   "POST",
        url:    "../OutPatient/PerformNextServiceAjax?set_ajax_view",
        data:   submitted + "&ticketId=" + ticketId,
        dataType:   "json",
        beforeSend: function(){},
        success:    function(data){
            if (data.error.code == 'OK') {
                if (data.result.nextRoom.length > 0) {
	                var msg =   _('Next location: <span class="bigImpression">{0}</span>', data.result.nextRoom);
	                objDialog.dialog('close');
	                generalDialog(null, null, msg);       
                }
                window.location = data.result.redirectLink;
            }
            else {
                displayError(data.error.mess);              
            }
        }
    });
}

function isReadyToSubmitNextService() {
    var status  =   false;
    if ($('#nextServiceFrm #serviceType').is(':checked')) {
        $("#nextServiceFrm input[name='serviceItemId']").each(function(){
            if ($(this).is(':checked')) {
                status  =   true;
            }
        });
    }
    else if ($('#nextServiceFrm #paraclinicalType').is(':checked')){
        $("#nextServiceFrm input[name='paraclinicalItemId\[\]']").each(function(){
            if ($(this).is(':checked')) {
                status  =   true;
            }
        });
    }
    
    if (!status) {
        $('#nextMsg').text(_('Please select next services to be performed')).show();
        return false;
    }
    else {
        $('#nextMsg').text(_('')).hide();
    }
    
    return true;
}

function refreshTicketContent() {
    var ticketId    =   $('#ticketIdInput').val();
    $.ajax({
        type:   "POST",
        url:    "../OutPatient/GetTicketContentAjax?set_ajax_view",
        data:   "ticketId=" + ticketId,
        dataType:   "json",
        beforeSend: function(){
            showAjaxLoader('ticketLoaderDiv', 'ticketRefreshBtnDiv', true);
        },
        success:    function(data){
            showAjaxLoader('ticketLoaderDiv', 'ticketRefreshBtnDiv', false);
            if (data.error.code == 'OK') {
                if (data.result.code == 'OK') {
                    $('#ticketItemListDiv').html(data.result.html);
                    $('#itemActionDiv').show();
                }
                else {
                    var msg =   _('Empty ticket');
                    $('#ticketItemListDiv').html("<div class='strong'>" +  msg + "</div>");
                    $('#goNextBtn').hide();
                }
            }
            else {
                displayError(data.error.mess);              
            }
        }
    });
}

function deleteTicketItem(ticketItemId) {
    $.ajax({
        type:   "POST",
        url:    "../OutPatient/ConfirmDeleteTicketItemAjax?set_ajax_view",
        data:   "ticketItemId=" + ticketItemId,
        dataType:   "json",
        beforeSend: function(){},
        success:    function(data){
            if (data.error.code == 'OK') {
                $('#ticketDialogDiv').html(data.result.html);
                var buttons =   {};
                var height = 100;
                buttons[_('Cancel')] = function(){
                    $(this).dialog('close');
                };
                if (data.result.code == 'CUR_OK') {
                    buttons[_('Ok')] = function(){
                        deleteItemAndContinue($(this));
                    };
                    height  =   350;
                }
                else if (data.result.code == 'CUR_ERR'){
                    buttons[_('Ok')] = function(){
                        deleteItemAndFinishTicket($(this));
                    };
                    height  =   250;
                }
                else {
                    buttons[_('Ok')] = function(){
                        deleteItem($(this));
                    };
                }
                generalDialog('ticketDialogDiv', _("Delete service"), null, null,{
                  autoOpen: true,         
                  bgiframe: true,
                  resizable: false,
                  modal: true,
                  width: 500,
                  height: height,
                  buttons: buttons
               });
            }
            else {
                displayError(data.error.mess);              
            }
        }
    });
}

function deleteItemAndContinue(objDialog) {    
    var submittedData   =   $('#deleteServiceFrm').formSerialize();
    $.ajax({
        type:   "POST",
        url:    "../OutPatient/deleteTicketItemAndGoNext?set_ajax_view",
        data:   submittedData,
        dataType:   "json",
        beforeSend: function(){
            objDialog.dialog('close');
            showAjaxLoader('ticketLoaderDiv', 'ticketRefreshBtnDiv', true);
        },
        complete: function(){
            showAjaxLoader('ticketLoaderDiv', 'ticketRefreshBtnDiv', false);
        },
        success:    function(data){
            if (data.error.code == 'OK') {
                refreshTicketContent();
            }
            else {
                displayError(data.error.mess);              
            }
        }
    });
}

function deleteItem(objDialog) {
    var submittedData   =   $('#deleteServiceFrm').formSerialize();
    $.ajax({
        type:   "POST",
        url:    "../OutPatient/DeleteTicketItemAjax?set_ajax_view",
        data:   submittedData,
        dataType:   "json",
        beforeSend: function(){
            objDialog.dialog('close');
            showAjaxLoader('ticketLoaderDiv', 'ticketRefreshBtnDiv', true);
        },
        complete: function(){
            showAjaxLoader('ticketLoaderDiv', 'ticketRefreshBtnDiv', false);
        },
        success:    function(data){
            if (data.error.code == 'OK') {
                refreshTicketContent();
            }
            else {
                displayError(data.error.mess);              
            }
        }
    });
}

function deleteItemAndFinishTicket(objDialog) {
	var submittedData   =   $('#deleteServiceFrm').formSerialize();
	$.ajax({
        type:   "POST",
        url:    "../OutPatient/deleteTicketItemAndGoNext?set_ajax_view",
        data:   submittedData,
        dataType:   "json",
        beforeSend: function(){
            objDialog.dialog('close');
            showAjaxLoader('ticketLoaderDiv', 'ticketRefreshBtnDiv', true);
        },
        complete: function(){
            showAjaxLoader('ticketLoaderDiv', 'ticketRefreshBtnDiv', false);
        },
        success:    function(data){
            if (data.error.code == 'OK') {
                if (data.result.nextRoom.length > 0) {
	            	var msg =   _('Next location: <span class="bigImpression">{0}</span>', data.result.nextRoom);
	                generalDialog(null, null, msg);       
                }
                window.location = data.result.redirectLink;
            }
            else {
                displayError(data.error.mess);              
            }
        }
    });
}

function addService(objDialog) {
	if (objDialog != null){
		objDialog.dialog('close');
	}
    var ticketId    =   $('#ticketIdInput').val();
    $.ajax({
        type:   "GET",
        url:    "../OutPatient/LoadServiceEditPageAjax?set_ajax_view",
        data:   "ticketId=" + ticketId,
        dataType:   "json",
        beforeSend: function(){},
        success:    function(data){
            if (data.error.code == 'OK') {
                $('#ticketDialogDiv').html(data.result.html);
                var buttons = {};
                buttons[_('Close')] = function(){
                    $(this).dialog('close');
                };
                var title =  _("Add service");
                if (data.result.ticketName.length > 0) {
                    title = data.result.ticketName + ": " + _("Add service"); 
                }
                
                generalDialog('ticketDialogDiv', title, null, null,{
                  autoOpen: true,         
                  bgiframe: true,
                  resizable: false,
                  modal: true,
                  width: 880,
                  height: 450,
                  buttons: buttons,
                  close: function() {
                        $('#ticketRefreshBtnDiv').trigger('click');
                  }
               });
            }
            else {
                displayError(data.error.mess);              
            }
        }
    });
}

function finishTicket(ticketId, objDialog) {
	if (objDialog != null) {
		objDialog.dialog('close');
	}
	$.ajax({
        type:   "GET",
        url:    "../OutPatient/finishTicketAjax?set_ajax_view",
        data:   "ticketId=" + ticketId,
        dataType:   "json",
        beforeSend: function(){
            showAjaxLoader('ticketLoaderDiv', 'ticketRefreshBtnDiv', true);
        },
        complete: function(){
            showAjaxLoader('ticketLoaderDiv', 'ticketRefreshBtnDiv', false);
        },
        success:    function(data){
            if (data.error.code == 'OK') {
                if (data.result.nextRoom.length > 0) {
	            	var msg =   _('Next location: <span class="bigImpression">{0}</span>', data.result.nextRoom);
	                generalDialog(null, null, msg);       
                }
                window.location = data.result.redirectLink;
            }
            else {
                displayError(data.error.mess);              
            }
        }
    });
}